<?php

class SocietyAction extends CommonAction
{
    protected static function str_trim($str)
    {
        $str = strim($str);
        $str = preg_replace("@<script(.*?)</script>@is", "", $str);
        $str = preg_replace("@<iframe(.*?)</iframe>@is", "", $str);
        $str = preg_replace("@<style(.*?)</style>@is", "", $str);
        return preg_replace("@<(.*?)>@is", "", $str);
    }

    // 公会列表
    public function index()
    {
        $where = 's.user_id = u.id';
        if (isset($_REQUEST['name'])) {
            $where .= ' and s.name like \'%' . addslashes($_REQUEST['name']) . '%\'';
        }
        if (isset($_REQUEST['nick_name'])) {
            $where .= ' and u.nick_name like \'%' . addslashes($_REQUEST['nick_name']) . '%\'';
        }
        if ($_REQUEST['begin_time']) {
            $where .= ' and s.create_time>=' . strtotime($_REQUEST['begin_time']);
        }
        if ($_REQUEST['end_time']) {
            $where .= ' and s.create_time<=' . strtotime($_REQUEST['end_time']);
        }
        if (!isset($_REQUEST['status'])) {
            $_REQUEST['status'] = -1;
        }
        if ($_REQUEST['status'] != -1) {
            $where .= ' and s.status=' . intval($_REQUEST['status']);
        }

        $model = M('society');
        $table = DB_PREFIX .'society s,'.DB_PREFIX .'user u';
        $count = $model->table($table)->where($where)->count();
        $p     = new Page($count, $listRows = 10);
        if ($count) {
            $field = 's.*,u.nick_name';
            $list  = $model->table($table)->where($where)->field($field)->order('s.id')->limit($p->firstRow . ',' . $p->listRows)->select();
            foreach ($list as $key => $value) {
                $list[$key]['create_time'] = to_date($value['create_time']);
                if($list[$key]['logo'] !=''){
                    $list[$key]['logo'] = get_spec_image($value['logo'],35,35);
                }
            }
        }

        $this->assign("page", $p->show());
        $this->assign("list", $list);
        $this->display();

    }

    //公会详情
    public function edit()
    {
        $id     = intval($_REQUEST['id']);
        $model  = M('society');
        $table  = DB_PREFIX .'society s,'.DB_PREFIX .'user u';
        $field  = 's.*,u.nick_name';
        $where  = 's.user_id = u.id and s.id=' . $id;
        $data = $model->table($table)->where($where)->field($field)->find();
        if ($data) {
            $data['create_time'] = to_date($data['create_time']);
            $data['logo'] = get_spec_image($data['logo']);
        }
        $this->assign('vo', $data);
        $this->display();
    }

    //更新公会信息
    public function update()
    {

        $id     = intval($_REQUEST['id']);
        $status = intval($_REQUEST['status']);
        $memo   = self::str_trim($_REQUEST['memo']);
        $manifesto   = self::str_trim($_REQUEST['manifesto']);
        $name   = self::str_trim($_REQUEST['name']);
        $this->assign("jumpUrl", u(MODULE_NAME . "/edit", array("id" => $id)));
        if (!$id) {
            $this->error("参数错误");
        }
        $modal      = M('society');
        $society   = $modal->field('name,user_id,status')->where("id=" . $id)->find();
        $log_info = $society['name'];
        $user_id  = $society['user_id'];
        $status   = $society['status'] == '1' ? 1 : $status;
        $res      = $modal->save(array('memo' => $memo,'name'=>$name,'manifesto'=>$manifesto,'status' => $status, 'id' => $id));


        M('user')->save(array('society_id' => $id, 'society_chieftain' => 1, 'id' => $user_id));

        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/BaseRedisService.php');
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
        $user_redis = new UserRedisService();
        $user_redis->update_db($user_id, array('society_id' => $id, 'family_chieftain' => 1));

        if (false === $res) {
            //错误提示
            save_log($log_info . L("UPDATE_FAILED"), 0);
            $this->error(L("UPDATE_FAILED"), 0, $log_info . L("UPDATE_FAILED"));
        } else {
            //成功提示
            clear_auto_cache("banner_list");
            load_auto_cache("banner_list");
            save_log($log_info . L("UPDATE_SUCCESS"), 1);
            $this->success(L("UPDATE_SUCCESS"));
        }
    }

    public function view()
    {
        $id     = intval($_REQUEST['id']);
        $model  = M('society');
        $field  = 'name';
        $where  = 'id=' . $id;
        $society = $model->where($where)->field($field)->find();
        $table  = DB_PREFIX .'society s,'.DB_PREFIX .'user u,'.DB_PREFIX .'society_apply sa';

        $where  = 'u.id =sa.user_id  and s.id ='.$id.' and  sa.society_id='.$id ;

        if (!isset($_REQUEST['status'])) {
            $_REQUEST['status'] = -1;
        }
        if ($_REQUEST['status'] != -1) {
            $where .= ' and sa.status=' . intval($_REQUEST['status']);
        } else {
            $where .= ' and sa.status<3';
        }

        $count = $model->table($table)->where($where)->count();
        $p     = new Page($count, $listRows = 20);
        if ($count) {
            $field = 's.*,u.nick_name,u.id,u.head_image,sa.society_id,sa.`status`';

            $list = $model->table($table)->where($where)->field($field)->order('s.id')->limit($p->firstRow . ',' . $p->listRows)->select();

            foreach ($list as $key => $value) {
                $list[$key]['create_time'] = to_date($value['create_time']);
                $list[$key]['head_image']  = get_spec_image($value['head_image']);
            }
        }

        $this->assign('id', $id);
        $this->assign('society', $society);
        $this->assign('list', $list);
        $this->assign("page", $p->show());

        $this->display();
    }

    public function income(){
        $m_config = load_auto_cache('m_config');
        $rate = floatval($m_config['society_public_rate']);

        $id = intval($_REQUEST['id']);
        $name = $_REQUEST['name'];

        $model = M('society');
        $field = 's.id,s.name,s.logo,s.memo,s.status,(u.ticket-u.refund_ticket) as ticket,((u.ticket-u.refund_ticket)*'.$rate.') as money';
        $table = DB_PREFIX."society as s,".DB_PREFIX."user as u";
        $where = 's.user_id=u.id';
        $parameter = 's.user_id=u.id';
        if ($id > 0){
            $where .= ' and s.id='.$id;
            $parameter .= '&s.id='.$id;
        }
        if ($name != ''){
            $where .= ' and s.name like \'%' . addslashes($name) . '%\'';
            $parameter .= '&s.name like \'%' . addslashes($name) . '%\'';
        }
        $count = $model->table($table)->where($where)->count();
        if ($count){
//            $p     = new Page($count, $listRows = 20);
//            $list = $model->table($table)->where($where)->field($field)->order('s.id')->select();

            $sql = "select $field from $table where $where order by s.id";
            $list = $this->_Sql_list($model,$sql,'&'.$parameter,'',0);

            foreach ($list as $key => $value){
                $list[$key]['logo']  = get_spec_image($value['logo']);
            }
        }
        $this->assign('list', $list);
//        $this->assign("page", $p->show());

        $this->display();
    }

    public function submit_refund(){
        $id = intval($_REQUEST['id']);
        if ($id == 0){
            $this->error("id不能为空");
        }

        $society_info = M('society')->where("id=".$id)->field('user_id,name')->find();
        $refund_exist = M('user_refund')->where('user_id='.$society_info['user_id']." and (is_pay =0 or is_pay=1)")->getField('id');
        if ($refund_exist){
            $this->error("这个公会还有未处理的提现");
        }
        $m_config = load_auto_cache("m_config");//初始化手机端配置
        $rate = floatval($m_config['society_public_rate']);
        $user_info = M('user')->where('id='.$society_info['user_id'])->field('ticket,refund_ticket')->find();
        $refund_data = array();
        $refund_data['ticket'] = $user_info['ticket'] - $user_info['refund_ticket'];
        if ($refund_data['ticket'] > 0){
            $refund_data['money'] = floatval($refund_data['ticket'] * $rate);
            $refund_data['user_bank_id'] = -1;
            $refund_data['user_id'] = $society_info['user_id'];
            $refund_data['create_time'] = NOW_TIME;
            $refund_data['memo'] = $society_info['name']."公会提现";
            $refund_data['withdrawals_type'] = 2;
            $refund_data['is_pay'] = 0;
            $res = M('user_refund')->add($refund_data);
            if ($res){
                save_log($society_info['name'] . "公会提现" . $refund_data['money'] . "成功", 1);
                $this->success("提现成功");
            }else{
                save_log($society_info['name'] . "公会提现" . $refund_data['money'] . "失败", 0);
                $this->success("提现失败");
            }
        }else{
            $this->error("此公会可提现余额为0");
        }

    }
}

